﻿Imports DotNetNuke.Common.Globals
Imports DotNetNuke.Entities.Portals
Imports DotNetNuke.Services
Imports DotNetNuke.Services.Search
Imports DotNetNuke.Services.FileSystem
Imports DotNetNuke.Security.Permissions
Imports System.Data
Imports Aricie.DNN.Settings
Imports Aricie.DNN.Modules.LuceneSearch.Business
Imports System.IO
Imports Aricie.DNN.Modules.LuceneSearch.Settings
Imports Aricie.DNN.Services
Imports Lucene.Net.Index
Imports Lucene.Net.Search
Imports System.Text.RegularExpressions
Imports System.Globalization

Namespace Aricie.DNN.Modules.LuceneSearch.ModuleProviders

    Public Class StandAloneTaxonomyProvider
        Implements ILuceneFieldGlossary
        Implements ILuceneStandAloneProgressiveSearchable



        Public Function GetSearchItems(ByVal portalId As Integer) As SearchItemInfoCollection Implements ILuceneStandAloneSearchable.GetSearchItems
            Dim settings As LucenePortalSettings = SettingsController.GetModuleSettings(Of LucenePortalSettings)(SettingsScope.PortalSettings, portalId)
            Dim toReturn As New SearchItemInfoCollection

            If settings IsNot Nothing AndAlso settings.EnableContentTaxonomyIndexation Then
                Dim SearchModule As DotNetNuke.Entities.Modules.ModuleInfo = NukeHelper.ModuleController.GetModuleByDefinition(NukeHelper.PortalId, Common.Constants.ModuleNameLuceneSearchResults)

                Dim terms = PortalQueryingDriver.GetCurrent().GetTopTerms(DnnSearchItemField.ContentTaxonomy, 0)
                For Each t As TermInfo In terms
                    Dim searchItem As New LuceneSearchItemInfo(portalId)
                    searchItem.Title = t.Text
                    searchItem.ItemType = DnnSearchItemField.ContentTaxonomy
                    searchItem.ItemGroup = DnnSearchItemField.ContentTaxonomy
                    searchItem.TabId = -1

                    Dim slugifiedText = ToSlug(t.Text)
                    searchItem.SearchKey = String.Format("{0}-{1}", DnnSearchItemField.ContentTaxonomy, slugifiedText)
                    searchItem.GUID = String.Format("{0}={1}", DnnSearchItemField.ContentTaxonomy, slugifiedText)
                    ' la notion de date de publication n'est pas forcément applicable ici, on va garder l'idée qu'elle est fixée à la date de l'indexation
                    searchItem.PubDate = DateTime.Now

                    Dim filterQuery As New BooleanQuery()
                    filterQuery.Add(New TermQuery(New Term(DnnSearchItemField.ContentTaxonomy, """" & t.Text & """")), Occur.MUST)
                    searchItem.ForcedUrl = DotNetNuke.Common.Globals.ResolveUrl(BusinessController.GetSearchUrl(SearchModule.TabID, String.Empty, filterQuery.ToString))

                    toReturn.Add(searchItem)
                Next

            End If

            Return toReturn

        End Function

        Public Function GetFieldDefinitions() As System.Collections.Generic.IList(Of Business.FieldDefinition) Implements Business.ILuceneFieldGlossary.GetFieldDefinitions
            Return Nothing
        End Function

        Private Function ToSlug(ByVal s As String) As String
            ' http://stackoverflow.com/questions/249087/how-do-i-remove-diacritics-accents-from-a-string-in-net
            Dim slug As String = s.Normalize(System.Text.NormalizationForm.FormD)
            Dim sb As New System.Text.StringBuilder(s.Length)
            For counter As Integer = 0 To slug.Length - 1
                Dim uc As UnicodeCategory = CharUnicodeInfo.GetUnicodeCategory(slug(counter))
                If (uc <> UnicodeCategory.NonSpacingMark) Then
                    sb.Append(slug(counter))
                End If
            Next

            slug = sb.ToString().Normalize(System.Text.NormalizationForm.FormC)
            slug = Regex.Replace(slug, "[\s-]+", " ").Trim()
            slug = Regex.Replace(slug, "\s", "-")
            Return slug
        End Function



        Public Sub UpgradeSearchItem(portalId As Integer, ByRef searchItem As Business.LuceneSearchItemInfo) Implements ILuceneSearchableUpgrade.UpgradeSearchItem

        End Sub
    End Class

End Namespace